package com.disease.chronic.Service.Impl;

import com.disease.chronic.Component.AlertWebSocket;
import com.disease.chronic.Exception.ChronicExceptionEnum;
import com.disease.chronic.Global.Result;
import com.disease.chronic.Mappers.IndicatorsDetailMapper;
import com.disease.chronic.Mappers.IndicatorsMapper;
import com.disease.chronic.Service.IndicatorDetailService;
import com.disease.chronic.dto.IndicatorDetailRequestDto;
import com.disease.chronic.pojo.Indicators;
import com.disease.chronic.pojo.IndicatorsDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.Date;

@Service
public class IndicatorDetailServiceImpl implements IndicatorDetailService {
    @Autowired
    IndicatorsDetailMapper indicatorsDetailMapper;

    @Autowired
    IndicatorsMapper indicatorsMapper;

    @Override
    public Result insertIndicatorDetail(IndicatorDetailRequestDto detailRequestDto) throws IOException {
        String username = detailRequestDto.getUsername();


        //今日时间
        Date today = new Date(System.currentTimeMillis());

        if(detailRequestDto.getBloodOxygen()!=null&& !detailRequestDto.getBloodOxygen().trim().isEmpty()){
            //进行报警判断
            int bloodOxy = Integer.parseInt(detailRequestDto.getBloodOxygen());
            if(bloodOxy<95){
                AlertWebSocket.alert(username,"血氧",detailRequestDto.getBloodOxygen(),1);
            }else{
                AlertWebSocket.alert(username,"血氧", detailRequestDto.getBloodOxygen(),0);
            }
            updateDetail(username,"血氧",today, detailRequestDto.getBloodOxygen());

        }
        if(detailRequestDto.getTemperature()!=null&& !detailRequestDto.getTemperature().trim().isEmpty()){
            double temp = Double.parseDouble(detailRequestDto.getTemperature());
            //报警判断
            if(temp<36||temp>37){
                AlertWebSocket.alert(username,"体温", detailRequestDto.getTemperature(),1);
            }else{
                AlertWebSocket.alert(username,"体温", detailRequestDto.getTemperature(),0);
            }
            updateDetail(username,"体温",today,detailRequestDto.getTemperature());
        }
        if(detailRequestDto.getHeartRate()!=null&& !detailRequestDto.getHeartRate().trim().isEmpty()){
            //进行报警判断
            int heartRate = Integer.parseInt(detailRequestDto.getHeartRate());
            if(heartRate <=40||heartRate>=160){
                AlertWebSocket.alert(username,"心率",detailRequestDto.getHeartRate(),1);
            }else{
                AlertWebSocket.alert(username,"心率", detailRequestDto.getHeartRate(),0);
            }
            updateDetail(username,"心率",today, detailRequestDto.getHeartRate());
        }
        if(detailRequestDto.getSystolicPressure()!=null&& !detailRequestDto.getSystolicPressure().trim().isEmpty()){
            //进行报警判断
            int sPressure = Integer.parseInt(detailRequestDto.getSystolicPressure());
            if(sPressure <90||sPressure>=140){
                AlertWebSocket.alert(username,"收缩压",detailRequestDto.getSystolicPressure(),1);
            }else{
                AlertWebSocket.alert(username,"收缩压", detailRequestDto.getSystolicPressure(),0);
            }
            updateDetail(username,"收缩压",today, detailRequestDto.getSystolicPressure());
        }
        if(detailRequestDto.getDiastolicPressure()!=null&& !detailRequestDto.getDiastolicPressure().trim().isEmpty()){
            //进行报警判断
            int dPressure = Integer.parseInt(detailRequestDto.getDiastolicPressure());
            if(dPressure <60||dPressure>=90){
                AlertWebSocket.alert(username,"舒张压",detailRequestDto.getDiastolicPressure(),1);
            }else{
                AlertWebSocket.alert(username,"舒张压", detailRequestDto.getDiastolicPressure(),0);
            }
            updateDetail(username,"舒张压",today, detailRequestDto.getDiastolicPressure());
        }

        return new Result(ChronicExceptionEnum.SUCCESS.getCode(),
                ChronicExceptionEnum.SUCCESS.getMessage());
    }

    //用来判断是更新数据还是插入数据
    public void updateDetail(String username,String indicatorName,Date day,String value){
        //查询今日是否已经有测量数据
        IndicatorsDetail indicatorsDetail = indicatorsDetailMapper.
                selectByUsernameAndIndicatorNameAndDay(username, indicatorName, day);

        //已有数据则更新，没有则插入
        if(indicatorsDetail!=null){
            indicatorsDetail.setValue(value);
            indicatorsDetailMapper.updateByPrimaryKey(indicatorsDetail);
        }else{
            //新增各项数据插入
            indicatorsDetail = new IndicatorsDetail();
            indicatorsDetail.setUsername(username);
            indicatorsDetail.setValue(value);
            indicatorsDetail.setDay(day);
            indicatorsDetail.setIndicatorName(indicatorName);
            indicatorsDetailMapper.insert(indicatorsDetail);
        }
        Indicators indicators = indicatorsMapper.selectByUsernameAndIndicatorName(username, indicatorName);
        if(indicators!=null){
            //如果指标已存在，更新最后一次测量值和最后一次测量日期
            indicators.setLastMeasure(value);
            //最后一次更新日期
            indicators.setLastUpdateTime(day);
            indicatorsMapper.updateByPrimaryKey(indicators);
        }else{
            //不存在就新建指标
            indicators = new Indicators();
            indicators.setLastUpdateTime(day);
            indicators.setIndicatorName(indicatorName);
            indicators.setUsername(username);
            indicators.setLastMeasure(value);

            //判断是什么指标，选择对应单位和范围
            if(indicatorName.equals("体温")){

                indicators.setIndicatorUnit("℃");
                indicators.setNormalScope("36-37");

            } else if (indicatorName.equals("血氧")) {

                indicators.setIndicatorUnit("%");
                indicators.setNormalScope("85-100");

            } else if (indicatorName.equals("心率")) {

               indicators.setIndicatorUnit("bpm");
               indicators.setNormalScope("60-100");

            } else if (indicatorName.equals("收缩压")) {

                indicators.setIndicatorUnit("mmHg");
                indicators.setNormalScope("90-140");

            }else if(indicatorName.equals("舒张压")){

                indicators.setIndicatorUnit("mmHg");
                indicators.setNormalScope("60-90");

            }
            indicatorsMapper.insert(indicators);
        }

    }
}
